package com.weiyuwqj.server.service.sys.impl;

import com.github.pagehelper.PageHelper;
import com.weiyuwqj.server.service.base.BaseServiceImpl;
import com.weiyuwqj.server.mapper.SysRoleMapper;
import com.weiyuwqj.api.service.SysRoleService;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.server.mapper.SysRoleResourceMapper;
import com.weiyuwqj.api.model.SysRole;
import com.weiyuwqj.api.model.SysRoleResource;
import com.weiyuwqj.api.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by zhangyong on 2016/7/20.
 */
@Service("roleService")
public class SysRoleServiceImpl extends BaseServiceImpl<SysRole> implements SysRoleService{

    @Autowired
    SysRoleMapper roleMapper;

    @Autowired
    SysRoleResourceMapper roleResourceMapper;

    //后台角色列表
    @Override
    public PageList getList(Paginator p, Map map) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        List<SysRole> list=roleMapper.getList(map);
        return new PageList(list);
    }

    //后台添加
    @Override
    public void add(SysRole role,String sources) {
        roleMapper.add(role);
        SysRoleResource resource=new SysRoleResource();
        resource.setRoleId(role.getId());
        if(!StringUtils.isNull(sources)){
            String[] s=sources.split(",");
            for(int i=0;i<s.length;i++){
                resource.setSourceId(Integer.parseInt(s[i]));
                roleResourceMapper.insert(resource);
            }
        }
    }

    //后台未分页列表
    @Override
    public List<SysRole> getList(Map map) {
        return roleMapper.getList(map);
    }
}
